var navActive = {
	ifNav : $('.footer-five-grid').length != 0,
	addActive : function(){
		if(this.ifNav){
			var footer = $('.footer-five-grid');
			footer.children('div').click(function(){
				$(this).addClass('active cl-f-orange').siblings().removeClass('active cl-f-orange');
			})
		}
	}
}
navActive.addActive();
//随手拍加载更多
var flagSsp = true;
var addload = {
   	isTrue : $('#scroll-wrapper').index() !== -1,
   	addssp : function(){
   		if(this.isTrue) {
   			if($('#scroll-wrapper').height()<$(window).height()-45){
   				$('.add-loading').remove();
   				return false;
   			}
   			var footH=$('.page-footer').height();
   			var addload = $('.add-loading');
			var classList=$('#scroll-wrapper #thelist li').get(0).className;
   			$thelist=$('#thelist');
    		$(window).on('scroll',function(){
    			var scrollPos = $(window).scrollTop(),wrapperH = $('#scroll-wrapper').height();
    			if(scrollPos > wrapperH - $(window).height() + footH){
    				if($('#thelist > li').length <= 10){ //有数据
    					var ele='<li class="'+classList+'">';
    					ele+=$('#scroll-wrapper #thelist li').html();
    					ele+='</li>';
    					$thelist.append(ele);
					}else { //数据为空
						if(flagSsp){
							addload.before('<p class="bottom-tips">— 已到最底部 —</p>').hide();
	    					flagSsp = false;
						}
    				}
    			}
    		}); 
   		}
   	}
}
//动态页加载更多
var flagDt = true;
var dtAddload = {
   	isTrue : $('.scroll-wrapper.active').index() !== -1,
   	additem : function(){
   		if(this.isTrue) {
   			if($('.scroll-wrapper.active').height()<$(window).height()-45){
   				$('.add-loading').remove();
   				return false;
   			}
   			var $wrapper=$('.scroll-wrapper.active');
   			var $thelist=$wrapper.find('.thelist');
   			var footH=$('.page-footer').height(); 
   			var addload = $wrapper.find('.add-loading');
   			var classList=$thelist.find('li').get(0).className;
    		$(window).on('scroll',function(){
    			var scrollPos = $(window).scrollTop(),
    				wrapperH = $wrapper.height();
    			if(scrollPos > wrapperH - $(window).height() + footH){
    				if($thelist.find('li').length <= 10){ //有数据
    					var ele='<li class="'+classList+'">';
    					ele+=$thelist.find('li').eq(0).html();
    					ele+='</li>';
    					$thelist.append(ele);
					}else { //数据为空
						if(flagSsp){
							addload.before('<p class="bottom-tips">— 已到最底部 —</p>').hide();
	    					flagSsp = false;
						}
    				}
    			}
    		}); 
   		}
   	}
}
function loading(){
	if($('.oimg').length>0){
		if($('.oimg').get(0).complete){
			$(".loading-wrapper").hide();
			addload.addssp();
		}
	}
	else{
		$(".loading-wrapper").hide();
		addload.addssp();
	}
}
// 跳转loading
document.onreadystatechange = loading;
 
$(document).ready(function() {
	function lazyloadImg(lazyItem){
		lazyItem.lazyload({
	    	effect : "fadeIn",
	    	threshold : 100,
	    	placeholder : "" //加载图片前占位
		});
	}
	lazyloadImg($('.lazy'));

	$('.clickable').click(function(event) {
		// 跳转
	});
	$('.like').click(function(event) {
		event.stopPropagation();
		var clickable=$(this).parents('.clickable');
		clickable.removeClass('clickable');
		var account=$(this).find('.like-text').text();
		$(this).find('.like-text').text(++account);
		setTimeout(function(){clickable.addClass('clickable');},500);
	});
	$('.ssp-item .follow').click(function(event){
		event.stopPropagation();
		event.preventDefault();
		var $tar=$(this);
		$tar.toggleClass('active');
		if($tar.hasClass('active')){
			$tar.text('已关注');
		}
		else $tar.text('+关注');
	});
	// 布局相关
	var preHeight=$('.index-ver-block').prev('.index-block').find('img').width();
	var verHeight=$('.index-ver-block').width()/2.09*2;
	$('.index-ver-block>div').css('marginBottom',preHeight-verHeight+'px');
	$('.index-ver-block>div:last').css('marginBottom',0);
	var sortW=($(window).width()-50)/4;
	$('.recipe-sort-type a').width(sortW);

	//comfirm样式
	window.alert = function(str){
        $('body').prepend('<div class="comfirm"><div class="comfirm-content"><div class="comfirm-body">'+
            str+'</div><div class="comfirm-footer"><button class="btn cl-b-gray">取消</button><button class="btn cl-b-orange cl-f-white">确定</button></div></div></div>');
        $('.comfirm').fadeIn();
        $('.comfirm-footer .btn').click(function(){
             $('.comfirm').fadeOut().remove();
        });
    };
    //返回顶部
	var flag = true;
	$(window).on('scroll',function(){
		var scrollPos = $(window).scrollTop();
		if(scrollPos > 0){
			if(flag){
				$('body').prepend('<div class="top">' +
					'<img src="./images/top.png" alt="">' +
				'</div>');
				$('.top').on('click',function(){
					$('html,body').animate({
						scrollTop: 0
					}, '500');
				}); 
				flag = false;
			}
			$('.top').removeClass('top-hide').addClass('top-show');
		}else {
			$('.top').removeClass('top-show').addClass('top-hide');
		}
	});  

	//随手拍发布删除图片
	$('.icon-del').on('click',delImg);
	//随手拍添加图片
	$('.poblish-img-add input').click(function(event) {
		var $parent=$(this).parent();
		var index=$parent.index();
		var src='images/poblish.png';
		var str='<li><div class="added-img" style="background-image:url('+src+')"><img src="../images/o2.png" class="oimg"></div><span class="del-img icon-del"></span></li>';
		$parent.before(str);
		$parent.parent().find('li').eq(index).find('.icon-del').on('click',delImg);
	});
	function delImg(){
		$(this).parent().animate({width:0,height:0},400,function(){
			$(this).remove();
		});
	}
	//随手拍详情评论数
	$countTxt=$('.comment-count');
	var commentNum=$countTxt.parents('.comment-list-wrapper').find('.comment-h1').length;
	$countTxt.text(commentNum);
	
	//随手拍点赞
	$('.ssp-blike').click(function(event) {
		event.preventDefault();
		$likeTxt=$(this).siblings('p');
		$likeNum=parseInt($likeTxt.text());
		$likeTxt.text($likeNum+1);
	});

	// 首页弹出导航
	$('.index-pop').click(function(event){
		$('.pop-nav').slideToggle('fast');
	});

	//回复评论
	function commentChoose(event){
		event.stopPropagation();
		$_this=$(this);
		$_this.parents('.comment-list-wrapper').find('.comment-h1').removeClass('comment-target');
		$_this.parents('.comment-list-wrapper').find('.comment-h2').removeClass('comment-target');
		$_this.addClass('comment-target');
		$('.send-comment-footer input').val('').attr('placeholder','');
		var user="@";
		switch(event.data.type){
			case 1:
				user+=$_this.find('p.comment-user').text();
				break;
			case 2:
				user+=$_this.find('span.comment-user').text().substring(1);
				break;
			default:
				user+='username';
		}
		$('.send-comment-footer input').attr('placeholder',user);
	}
	
	$('.comment-h1').on('click',{type:1},commentChoose);
	$('.comment-h2').on('click',{type:2},commentChoose);

	$('.send-comment-submit').click(function(event) {
		event.preventDefault();
		if($('.comment-target').hasClass('comment-h1')){
			var str='<div class="comment-h2 clickable"><span class="cl-f-lorange comment-user">@'
			str+='username';
			str+='</span>';
			str+=$(this).siblings('input').val();
			str+='</div>';
			$('.comment-target').find('.comment-content').append(str);
			$('.comment-target').find('.comment-content').children(':last').on('click',{type:2},commentChoose);
		}
		else if($('.comment-target').hasClass('comment-h2')){
			var str='<div class="comment-h2 clickable"><span class="cl-f-lorange comment-user">@'
			str+='username';
			str+='</span>';
			str+=$(this).siblings('input').val();
			str+='</div>';
			$('.comment-target').parent().children(':last').after(str);
			$('.comment-target').parent().children(':last').on('click',{type:2},commentChoose);
		}
		else{
			var str='<li class="ub comment-h1 clickable row"><img src="images/touxiang.png" class="tx-cir lazy">';
			str+='<div class="comment-text ub-f1"><p class="cl-f-lorange comment-user">';
			str+='username';
			str+='</p><p class="cl-f-lgrey comment-time">2015-12-01<span>13:10</span></p>';
			str+='<div class="cl-f-grey comment-content"><p>';
			str+=$(this).siblings('input').val();
			str+='</p></div></div></li>';
			$('.comment-list-wrapper').append(str);
			$('.comment-list-wrapper').find('.comment-h1:last').on('click',{type:1},commentChoose);
		}
		$('.send-comment-footer input').val('');
		$('.comment-list-wrapper').find('.comment-h1').removeClass('comment-target');
		$('.comment-list-wrapper').find('.comment-h2').removeClass('comment-target');
	});
	$('.comment-list-title,.ssp-detail-wrapper,.sp-wrapper-item,.empty-input').click(function(){
		$('.send-comment-footer input').attr('placeholder','');
	});
	
	//动态tab切换
	$('.tab-section a').click(function(event) {
		event.preventDefault();
		$_this=$(this);
		var index=$_this.parent().index();
		var pos=index*0.25*100;
		$('.scroll-wrapper').removeClass('active').eq(index).addClass('active');
		$(this).parent().siblings().find('a').removeClass('active');
		$(this).addClass('active');
		$('.tab-line').css('left',pos+'%');
		dtAddload.additem();
	});

	//食谱发布选择类别
	$('.recipe-type-list li').click(function(event) {
		var _this=$(this);
		$('.alert-type-layer').fadeIn();
		$('.alert-type-content').removeClass('alert-slideOut').addClass('alert-slideIn');
		$('.alert-type-cancel').one('click',function(){
			$('.alert-type-content').removeClass('alert-slideIn').addClass('alert-slideOut');
			$('.alert-type-layer').fadeOut();
		});
		$('.alert-type-list li').click(function(event) {
			var target=$(this).text();
			_this.siblings().find('span').text('').addClass('icon-drop');
			_this.find('span').removeClass('icon-drop').text(target);
			$(this).parent().find('li').removeClass('active');
			$(this).addClass('active');
			$('.recipe-type-section input').val(target);
			$('.alert-type-cancel').trigger('click');
		});
	});

	//食谱发布添加删除材料、步骤等按钮
	function minusRow(){
		$(this).parent().remove();
	}
	$('.recipe-poblish-addrow').click(function(event) {
		var eleClass=$(this).siblings('li').get(0).className;
		var ele='<li class="'+eleClass+'">';
		ele+=$(this).siblings('li').html();
		ele+='</li>';
		$(this).before(ele);
		$(this).prev().find('.recipe-material-minus').on('click',minusRow);
	});
	$('.recipe-material-minus').on('click',minusRow);

	$('.daoshi-page-name').click(function(){
		$(this).find('.daoshi-choose').slideToggle('fast');
	})
	$('.daoshi-choose-item').click(function(e){
		e.stopPropagation();
		$(this).addClass('active').siblings().removeClass('active');
	})
	$('.icon-daoshi-minus').on('click',delImg);
	$('.daoshi-fabu-add').eq(0).click(function(){
		$('.daoshi-fabu-content').append('<p class="daoshi-content-text"><input type="text" class="form-control" placeholder="1、请输入文字描述"><span class="icon-daoshi-minus"></span></p>').find('.icon-daoshi-minus').on('click',delImg);
	})
	$('.daoshi-fabu-add').eq(1).click(function(){
		$('.daoshi-fabu-content').append('<div class="add-daoshi-info">' +
					'<span class="icon-daoshi-minus"></span>' +
					'<div class="ub ub-ver ub-ac poblish-img-addtxt">' +
						'<span class="icon-daoshi-plus"></span>' +
						'<p>添加图片</p>' +
					'</div>' +
				'</div>').find('.icon-daoshi-minus').on('click',delImg);
	});
	$(function() {
	    FastClick.attach(document.body);
	});
	$('.clickable,.clickable-deep').on('click',function(e) {
		var $_this = $(this);
		$_this.addClass('active');
		setTimeout(function(){
			$_this.removeClass('active');
		}, 150)
	})
});
    
    
